﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using System.IO;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Basics.Shop.MemberCard
{
    public partial class MemberCardUserList : System.Web.UI.Page
    {

        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
            
            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

            string sql = " from WxMemberCard where OwnerAccount=:acc ";

            QueryFilterList filters = new QueryFilterList();
            if (!string.IsNullOrEmpty(Request["account"]))
            {
                filters.Add(new StringFilter("AccountId", "%" + Request["account"] + "%", StringCompairType.Like));
            }
            if (!string.IsNullOrEmpty(Request["name"]))
            {
                filters.Add(new StringFilter("MemberName", "%" + Request["name"] + "%", StringCompairType.Like));
            }
            foreach (DataFilter filter in filters)
            {
                sql += " and " + filter.GetSection();
            }

            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                    IQuery query = session.CreateQuery("select count(*)" + sql)
                        .SetParameter("acc", UserKit.GetUserAccountFailLogin());
                    filters.SetUp(query);

                    //设置记录总数
                    _framePage.RecordCount = Convert.ToInt32(query.UniqueResult());

                    query = session.CreateQuery(sql)
                        .SetParameter("acc", UserKit.GetUserAccountFailLogin())
                        .SetFirstResult((pageIndex - 1) * pageSize)
                        .SetMaxResults(pageSize);
                    filters.SetUp(query);

                    //设置数据源
                    repeater1.DataSource = query.List<WxMemberCard>();

                }
            repeater1.DataBind();
        }
        

    }
}